import request from "@/utils/request";
import { API_ENDPOINTS } from "@/config/api";

// 用户信息接口
export interface CommunityUser {
  id: number;
  username: string;
  avatar_url?: string;
  level: number;
  bio?: string;
}

// 图片接口
export interface CommunityImage {
  id: number;
  url: string;
  thumbnail_url?: string;
  alt_text?: string;
}

// 社区分享接口
export interface CommunityShare {
  id: number;
  user_id: number;
  user: CommunityUser;
  title: string;
  content: string;
  images?: CommunityImage[];
  tags?: string[];
  share_type: "achievement" | "experience" | "question";
  is_public: boolean;
  likes_count: number;
  comments_count: number;
  shares_count: number;
  views_count?: number;
  is_liked: boolean;
  is_bookmarked: boolean;
  created_at: string;
  updated_at: string;
}

// 社区互动接口
export interface CommunityInteraction {
  id: number;
  user: {
    id: number;
    username: string;
  };
  interaction_type: "like" | "comment";
  content?: string;
  created_at: string;
}

// 求助回答接口
export interface HelpAnswer {
  id: number;
  user: CommunityUser;
  content: string;
  is_best_answer: boolean;
  likes_count: number;
  created_at: string;
}

// 求助请求接口
export interface HelpRequest {
  id: number;
  user_id: number;
  user: CommunityUser;
  title: string;
  description: string;
  category?: string;
  tags?: string[];
  reward_points?: number;
  status: "open" | "answered" | "solved" | "closed";
  answers_count: number;
  views_count: number;
  best_answer_id?: number;
  best_answer?: HelpAnswer;
  is_bookmarked: boolean;
  created_at: string;
  updated_at: string;
}

// 创建分享参数
export interface CreateShareParams {
  content: string;
  share_type: "achievement" | "experience" | "question";
  is_public?: boolean;
}

// 互动参数
export interface InteractParams {
  interaction_type: "like" | "comment";
  content?: string;
}

// 创建求助请求参数
export interface CreateHelpRequestParams {
  title: string;
  description: string;
  category: "conversation" | "confidence" | "social_skills" | "other";
}

// 创建回答参数
export interface CreateAnswerParams {
  content: string;
}

// 社区群组接口
export interface CommunityGroup {
  id: number;
  name: string;
  description?: string;
  avatar?: string; // 群组头像
  cover_image?: string; // 群组封面
  members_count: number;
  posts_count: number;
  activity_level: "Low" | "Medium" | "High";
  is_joined: boolean;
  is_private: boolean;
  is_verified: boolean;
  tags?: string[];
  category_id?: number;
  latest_activity?: {
    text: string;
    time: string;
  };
  joining?: boolean; // 用于UI状态管理
  created_at?: string;
  updated_at?: string;
}

// 社区相关API
export const communityApi = {
  // 获取社区分享列表
  getShares(params?: { share_type?: string; page?: number }) {
    return request.get<{
      current_page: number;
      data: CommunityShare[];
      first_page_url: string;
      from: number;
      last_page: number;
      last_page_url: string;
      next_page_url: string | null;
      path: string;
      per_page: number;
      prev_page_url: string | null;
      to: number;
      total: number;
    }>(API_ENDPOINTS.COMMUNITY.SHARES, { params });
  },

  // 创建分享
  createShare(params: CreateShareParams) {
    return request.post<CommunityShare>(API_ENDPOINTS.COMMUNITY.SHARES, params);
  },

  // 与分享互动
  interactWithShare(shareId: number, params: InteractParams) {
    return request.post(
      API_ENDPOINTS.COMMUNITY.SHARE_INTERACT(shareId),
      params
    );
  },

  // 获取分享评论
  getShareComments(shareId: number) {
    return request.get<CommunityInteraction[]>(
      API_ENDPOINTS.COMMUNITY.SHARE_COMMENTS(shareId)
    );
  },

  // 获取求助请求列表
  getHelpRequests(params?: {
    status?: string;
    category?: string;
    page?: number;
  }) {
    return request.get<HelpRequest[]>(API_ENDPOINTS.COMMUNITY.HELP_REQUESTS, {
      params,
    });
  },

  // 创建求助请求
  createHelpRequest(params: CreateHelpRequestParams) {
    return request.post<HelpRequest>(
      API_ENDPOINTS.COMMUNITY.HELP_REQUESTS,
      params
    );
  },

  // 回答求助请求
  answerHelpRequest(requestId: number, params: CreateAnswerParams) {
    return request.post<HelpAnswer>(
      API_ENDPOINTS.COMMUNITY.HELP_ANSWERS(requestId),
      params
    );
  },

  // 标记最佳答案
  markBestAnswer(answerId: number) {
    return request.put(API_ENDPOINTS.COMMUNITY.MARK_BEST_ANSWER(answerId));
  },

  // 获取群组列表
  getGroups(params?: {
    category?: string;
    filter?: string;
    search?: string;
    page?: number;
  }) {
    return request.get<{
      current_page: number;
      data: CommunityGroup[];
      last_page: number;
      total: number;
    }>("/api/community/groups", { params });
  },

  // 创建群组
  createGroup(params: {
    name: string;
    description?: string;
    avatar?: string;
    is_private?: boolean;
    require_approval?: boolean;
    allow_invites?: boolean;
    category_id?: number;
    tags?: string[];
    invite_emails?: string[];
    rules?: string;
  }) {
    return request.post<CommunityGroup>("/api/community/groups", params);
  },

  // 加入群组
  joinGroup(groupId: number) {
    return request.post(`/api/community/groups/${groupId}/join`);
  },

  // 离开群组
  leaveGroup(groupId: number) {
    return request.delete(`/api/community/groups/${groupId}/leave`);
  },
};
